<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/16
 * Time: 15:06
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;

class Rbac extends Controller
{
    protected $name = 'think_user';
//    模块名
    private $mo="";
//    控制器
    private $co="";
//    方法名
    private $act="";
//    判断是否登录 以及 获取 方法名 控制器名 模块名
    public function _initialize(){
        $request = request();
//        var_dump($request);
        //        获取模块名
        $this->mo = $request->module();
        //        获取控制其名
        $this->co = $request->controller();
        //        获取方法名
        $this->act = $request->action();
        $root = $this->mo . '/' . $this->co . '/' . $this->act;

        $pp=$request->path();
        $canshu=explode("/",$pp);
//        var_dump($canshu);
        $str="";
        for($i=3;$i<count($canshu);$i++){
            if($i+1!=count($canshu)){
                $str.="?";
                $str.=$canshu[$i];
                $str.="=";
                $str.=$canshu[$i+1];
                if($i+1!=count($canshu)){
                    $str.="&";
                }else{

                }
            }
        }
        switch($root){
            case "admin/Open/test":
                $_SESSION["url"]="admin/Open/open";
                break;
            case "admin/Spread/test":
                $_SESSION["url"]="admin/Spread/spread";
                break;
            case "admin/Phone/test":
                $_SESSION["url"]="admin/Phone/phone";
                break;
            case "admin/Desk/test":
                $_SESSION["url"]="admin/Desk/desk";
                break;
            case "admin/Advisory/test":
                $_SESSION["url"]="admin/Advisory/advisory";
                break;
            case "admin/Teachp/test":
                $_SESSION["url"]="admin/Teachp/teachp";
                break;
            case "admin/Teachs/test":
                $_SESSION["url"]="admin/Teachs/teachs";
                break;
            case "admin/Prep/test":
                $_SESSION["url"]="admin/Prep/prep";
                break;
            default:
                $_SESSION["url"]=$root.$str;
        }
        //var_dump($_SESSION["url"]);
//        exit;
        if (!empty($_SESSION['u_name'])) {
            $name = $_SESSION['u_name'];
            $result = Db::name("user")
                ->alias("u")
                //            加入角色表
                ->join("role r", "r.r_id=u.user_r_id")
                //            缴入权限_角色关系表
                ->join("role_perssion rp", "rp.r_id=r.r_id")
                ->where("user_name='$name'")
                ->find();
            //            将查到的p_id转成一个数组
            $pid = explode(",", $result['p_id']);
            $res = [];
            //            循环$pid的每一项从 权限菜单关联表 中查询m_id
            for ($i = 0; $i < count($pid); $i++) {
                $res[] = Db::name("perssion")
                    ->alias("p")
                    ->join("perssion_menu pm", "pm.p_id=p.p_id")
                    ->where("p.p_id='$pid[$i]'")
                    ->select();
            }
            //            处理查到的m_id
            $mid = [];
            foreach ($res as $k => $v) {
                $mid[] = $v[0]["m_id"];
            }
            $mi = implode($mid, ",");
            $mi1 = array_values(array_unique(explode(",", $mi)));
            //          根据id 从 menu 表 中 查询
            $menu = [];
            for ($i = 0; $i < count($mi1); $i++) {
                $menu[] = Db::name("menu")
                    ->where("m_id='$mi1[$i]'")
                    ->find();
            }
            //var_dump($menu);
            //          组合路径并 保存到 rows 数组中
            //$rows = [];
            foreach ($menu as $k => $v) {
                $rows[] = $v["m_model"] . '/' . $v["m_controller"] . '/' . $v["m_method"];
            }
           // var_dump($rows);
            if (in_array($root, $rows)) {

            } else {
                echo "<script>alert('您没有访问权限！'); location.href='/admin/index/main'</script>";
                exit;
            }
        } else {
            $this->error("您还没有登录", 'admin/login/login');
        }
    }
    //查询该用户可以访问的模块、控制器和方法
    public function select(){

    }
}